package org.louie.hypercube;

import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;

import static org.lwjgl.glfw.GLFW.GLFW_PRESS;

public class HypercubeRenderer {
    private long window;
    private int width = 1080;
    private int height = 1080;



    public void run() {
        init();
        loop();
        cleanup();
    }

    private void init() {
        // 设置错误回调
        GLFWErrorCallback.createPrint(System.err).set();

        // 初始化 GLFW
        if (!GLFW.glfwInit()) {
            throw new IllegalStateException("无法初始化 GLFW");
        }

        // 创建窗口
        window = GLFW.glfwCreateWindow(width, height, "4D Hypercube", 0, 0);
        if (window == 0) {
            throw new RuntimeException("窗口创建失败");
        }

        // 让 OpenGL 运行在当前线程
        GLFW.glfwMakeContextCurrent(window);
        GL.createCapabilities();
    }

    private void loop() {

/*        Cube3D cube3D = new Cube3D();
        while (!GLFW.glfwWindowShouldClose(window)) {
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
            cube3D.drawRotatingCube();
            GLFW.glfwSwapBuffers(window);
            GLFW.glfwPollEvents();
        }*/

        Cube4D cube4D = new Cube4D();
        // 渲染循环
        while (!GLFW.glfwWindowShouldClose(window)) {
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
            // 这里之后绘制 4D 魔方
//            cube4D.drawRotatingHypercube();
//            cube4D.drawRotatingHypercube2();
            cube4D.drawRotatingHypercube3();

            cube4D.processInput(window);
            GLFW.glfwSwapBuffers(window);
            GLFW.glfwPollEvents();
        }
    }

    private void cleanup() {
        GLFW.glfwDestroyWindow(window);
        GLFW.glfwTerminate();
    }

    public static void main(String[] args) {
        new HypercubeRenderer().run();
    }
}
